<template>
  <el-row style="width: 100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ $t('departments.operation') }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepts } from "@/api/departments";
export default {
  // 它既可以写数组又可以写对象，但是如果写对象可以做一些限制
  props: {
    // 代表父传过来的属性名叫treeNode  相当于：props:['treeNode']
    treeNode: {
      // 限制类型为对象类型
      type: Object,
      required: true,
    },

    isRoot: {
      type: Boolean,
      // 设置它的默认值
      default: false,
    },
  },

  methods: {
    handleCommand(cmd) {
      // switch对于固定的值，比if的效率更高
      switch (cmd) {
        case "add":
          // 做新增
          this.$emit('addDept', this.treeNode.id)
          break;
        case "edit":
          // 做修改
          this.$emit('editDept', this.treeNode.id)
          break;
        case "del":
          // 做删除
          // 先弹出一个确认框

          this.$confirm("请问确定要删除吗？", "温馨提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
          })
            .then(async () => {
              await delDepts(this.treeNode.id);
              this.$message.success(this.treeNode.name + "删除成功！");
              // 再发一个请求去获取最新的部门列表
              this.$emit('delDept')
            })

          break;
      }
    },
  },
};
</script>

<style>
</style>